package cms.api.common.cofiguration;

import cms.api.framework.search.IndexService;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.solr.SolrProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

/**
 * Solr索引服务配置
 *
 * @author heyingcheng
 * @email heyingcheng@ctvit.com.cn
 * @date 2019/7/9 15:57
 */
@Configuration
@ConditionalOnClass({ HttpSolrClient.class, CloudSolrClient.class })
@EnableConfigurationProperties(SolrProperties.class)
public class SolrAipConfiguration {

    private final SolrProperties properties;

    public SolrAipConfiguration(SolrProperties properties) {
        this.properties = properties;
    }

    private IndexService indexService;

    @Autowired
    @Qualifier("solrCollectionServiceImpl")
    private IndexService solrCollectionService;
    @Autowired
    @Qualifier("solrCoreServiceImpl")
    private IndexService solrCoreService;

    @Bean
    public IndexService indexService() {
        this.indexService = createSolrIndexService();
        return this.indexService;
    }

    private IndexService createSolrIndexService() {
        if (StringUtils.hasText(this.properties.getZkHost())) {
            return solrCollectionService;
        }
        return solrCoreService;
    }


}
